//约定试路由
import { createRouter, createWebHistory } from 'vue-router'

// 通过目录的层级结构来生成路由
let metainfo = import.meta.glob("../view/**/index.js", {
    eager: true,
    import: "default"
})

let pathinfo = import.meta.glob("../view/**/index.vue", {})
console.log( import.meta.glob("../view/**", {}),"sjhgkjskjgfh")



// 给个路径 返回一个新的路径
let simplifyPath = (path) => {
    // 去掉开头的 "../view" 并去掉结尾的 "/index.js"
    let newPath = path.replace(/^\.\.\/view/, '').replace(/\/index\.js$/, '');

    // 添加一个前导斜杠
    return newPath;
}

let routerPath = Object.entries(metainfo).map((item) => {

    let [path, meta] = item
    console.log(path, meta, "oo") //数组的解构赋值
    console.log(pathinfo['../view/about/index.vue'])
    return {
        path: simplifyPath(path),
        name: simplifyPath(path).split('/').pop(),
        component: pathinfo[path.replace(/\.js$/, ".vue")],
        meta: meta
    }

})
console.log(routerPath)
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: routerPath
})
// export routerPath
export default {
    // 这是路由
    router,
    // 自动生成的路径
    routerPath

}